package com.csun.cmny.provider.device.phone;

import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.netty.NettyInitRunner;
import io.netty.bootstrap.ServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-04-13 19:04
 * @Description:
 */
@Slf4j
@Order(15)
@Configuration
@ConditionalOnProperty(prefix = "cmny.netty.phone", name = "switch", havingValue = "on")
public class PhoneNettyConfig {

    @Resource
    private CmnyProperties cmnyProperties;

    @Resource
    private PhoneNettyTcpChannelHandler phoneNettyTcpChannelHandler;

    @Bean
    public PhoneNettyTcpServer phoneNettyTcpServer() {

        int port = cmnyProperties.getNetty().getPhone().getPort();

        log.info("###PhoneInitRunner starting...<<<<<<<<<<<<<");

        PhoneNettyTcpServer phoneNettyTcpServer =
                new PhoneNettyTcpServer(port, phoneNettyTcpChannelHandler, new ServerBootstrap());

        phoneNettyTcpServer.init();

        NettyInitRunner.nettyServerMap.put(port, phoneNettyTcpServer);

        log.info("###PhoneInitRunner finish<<<<<<<<<<<<<");

        return phoneNettyTcpServer;
    }
}
